﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using Microsoft.Practices.EnterpriseLibrary.Common;
using System.Configuration;
using Insifo.BE.Gen;

namespace Insifo.DL.Gen
{
    public class DLDiccionario
    {
        #region  Members

        /// <summary>
        /// Método obtiene los Origenes de Datos Activos
        /// </summary>
        /// <returns>Devuelve un DataSet</returns>
        public DataSet Listar()
        {
            DataSet ds = new DataSet();
            SqlDatabase sqlDB = new SqlDatabase(CadenaConexion.Obtener());
            DbCommand dbCmd = sqlDB.GetStoredProcCommand("uSP_INSIFO_GEN_Diccionario_Obtener");
            dbCmd.CommandTimeout = CadenaConexion.CommandTimeout;
            try
            {
                ds = sqlDB.ExecuteDataSet(dbCmd);
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {                
                //if (dbCmd.Connection.State == ConnectionState.Open)
                    //dbCmd.Connection.Close();
                dbCmd.Dispose();                
            }
            return ds;
        }

        /// <summary>
        /// Método obtiene los Diccionarios
        /// </summary>
        /// <returns>Devuelve un DataSet</returns>
        public BEDiccionario Obtener(int pintCodigoTexto)
        {
            BEDiccionario be = new BEDiccionario();
            SqlDatabase sqlDB = new SqlDatabase(CadenaConexion.Obtener());
            DbCommand dbCmd = sqlDB.GetStoredProcCommand("uSP_INSIFO_GEN_Diccionario_Listar");
            dbCmd.CommandTimeout = CadenaConexion.CommandTimeout;
            try
            {
                sqlDB.AddInParameter(dbCmd, "@ai_CodigoTexto", DbType.Int32, pintCodigoTexto);
                using (IDataReader dr = sqlDB.ExecuteReader(dbCmd))
                {
                    while (dr.Read())
                    {
                        be.ESP = DBValue.Get<string>(dr, "ESP");
                        be.ENG = DBValue.Get<string>(dr, "ENG");
                        be.FRA = DBValue.Get<string>(dr, "FRA");
                        break;
                    }
                }
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                //if (dbCmd.Connection.State == ConnectionState.Open)
                //dbCmd.Connection.Close();
                dbCmd.Dispose();
            }
            return be;
        }


        #endregion  Members
    }
}
